`:top
In `F33f`_`[mathematics`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Mathematics]`_`f and in `F33f`_`[computer programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_programming]`_`f, a `!variadic function`! is a `F33f`_`[function`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Function_(programming)]`_`f of indefinite `F33f`_`[arity`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Arity]`_`f, i.e., one which accepts a variable number of `F33f`_`[arguments`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Argument_(computer_science)]`_`f. Support for variadic functions differs widely among `F33f`_`[programming languages`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Programming_language]`_`f.
The term `*variadic`* is a `F33f`_`[neologism`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Neologism]`_`f, dating back to 1936/1937.`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f] The term was not widely used until the 1970s.
>>Contents
• `F0af`_`[Overview`#overview]`_`f
• `F0af`_`[Examples`#examples]`_`f
• `F0af`_`[In C`#in-c]`_`f
• `F0af`_`[In C#`#in-c]`_`f
• `F0af`_`[In C++`#in-c]`_`f
• `F0af`_`[In Fortran`#in-fortran]`_`f
• `F0af`_`[In Go`#in-go]`_`f
• `F0af`_`[In Java`#in-java]`_`f
• `F0af`_`[In JavaScript`#in-javascript]`_`f
• `F0af`_`[In Lua`#in-lua]`_`f
• `F0af`_`[In Pascal`#in-pascal]`_`f
• `F0af`_`[In PHP`#in-php]`_`f
• `F0af`_`[In Python`#in-python]`_`f
• `F0af`_`[In Raku`#in-raku]`_`f
• `F0af`_`[In Ruby`#in-ruby]`_`f
• `F0af`_`[In Rust`#in-rust]`_`f
• `F0af`_`[In Scala`#in-scala]`_`f
• `F0af`_`[In Swift`#in-swift]`_`f
• `F0af`_`[In Tcl`#in-tcl]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[Notes`#notes]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f
-─
>>Overview
There are many mathematical and logical operations that come across naturally as variadic functions. For instance, the summing of numbers or the `F33f`_`[concatenation`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Concatenation]`_`f of strings or other sequences are operations that can be thought of as applicable to any number of operands (even though formally in these cases the `F33f`_`[associative property`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Associative_property]`_`f is applied).
Another operation that has been implemented as a variadic function in many languages is output formatting. The `F33f`_`[C`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_(programming_language)]`_`f function `F33f`_`[printf`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Printf]`_`f and the `F33f`_`[Common Lisp`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Common_Lisp]`_`f function `F33f`_`[format`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Format_(Common_Lisp)]`_`f are two such examples. Both take one argument that specifies the formatting of the output, and `*any number`* of arguments that provide the values to be formatted.
Variadic functions can expose `F33f`_`[type-safety`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_safety]`_`f problems in some languages. For instance, C's `B100`F9d9printf`f`b, if used incautiously, can give rise to a class of security holes known as `F33f`_`[format string attacks`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Format_string_attack]`_`f. The attack is possible because the language support for variadic functions is not type-safe: it permits the function to attempt to pop more arguments off the `F33f`_`[stack`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Stack_(abstract_data_type)]`_`f than were placed there, corrupting the stack and leading to unexpected behavior. As a consequence of this, the `F33f`_`[CERT Coordination Center`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=CERT_Coordination_Center]`_`f considers variadic functions in C to be a high-severity security risk.`:cite-ref-2[`F5bf`_`[2`#cite-note-2]`_`f]
In `F33f`_`[functional programming`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Functional_programming]`_`f languages, variadics can be considered complementary to the `F33f`_`[apply`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Apply]`_`f function, which takes a function and a list/sequence/array as arguments, and calls the function with the arguments supplied in that list, thus passing a variable number of arguments to the function. In the functional language `F33f`_`[Haskell`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Haskell]`_`f, variadic functions can be implemented by returning a value of a `F33f`_`[type class`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Type_class]`_`f `B100`F9d9T`f`b; if instances of `B100`F9d9T`f`b are a final return value `B100`F9d9r`f`b and a function `B100`F9d9(T t) => x -> t`f`b, this allows for any number of additional arguments `B100`F9d9x`f`b.
A related subject in `F33f`_`[term rewriting`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Term_rewriting]`_`f research is called `!hedges`!, or `!hedge variables`!.`:cite-ref-3[`F5bf`_`[3`#cite-note-3]`_`f] Unlike variadics, which are functions with arguments, hedges are sequences of arguments themselves. They also can have constraints ('take no more than 4 arguments', for example) to the point where they are not variable-length (such as 'take exactly 4 arguments') - thus calling them `*variadics`* can be misleading. However they are referring to the same phenomenon, and sometimes the phrasing is mixed, resulting in names such as `*variadic variable`* (synonymous to hedge). Note the double meaning of the word `*variable`* and the difference between arguments and variables in functional programming and term rewriting. For example, a term (function) can have three variables, one of them a hedge, thus allowing the term to take three or more arguments (or two or more if the hedge is allowed to be empty).
>>Examples
>>>In C
To portably implement variadic functions in the `F33f`_`[C language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_(programming_language)]`_`f, the standard `F33f`_`[stdarg.h`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Stdarg.h]`_`f header file is used. The older `F33f`_`[varargs.h`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Varargs.h]`_`f header has been `F33f`_`[deprecated`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Deprecation]`_`f in favor of `B100`F9d9stdarg.h`f`b. In C++, the header file `B100`F9d9cstdarg`f`b is used.`:cite-ref-4[`F5bf`_`[4`#cite-note-4]`_`f]
`B100`F9d9#include <stdarg.h>`f`b
`B100`F9d9#include <stdio.h>`f`b
`B100`F9d9`f`b
`B100`F9d9double average(int count, ...) {`f`b
`B100`F9d9 va_list ap;`f`b
`B100`F9d9 int j;`f`b
`B100`F9d9 double sum = 0;`f`b
`B100`F9d9`f`b
`B100`F9d9 va_start(ap, count); /* Before C23: Requires the last fixed parameter (to get the address) */`f`b
`B100`F9d9 for (j = 0; j < count; j++) {`f`b
`B100`F9d9 sum += va_arg(ap, int); /* Increments ap to the next argument. */`f`b
`B100`F9d9 }`f`b
`B100`F9d9 va_end(ap);`f`b
`B100`F9d9`f`b
`B100`F9d9 return sum / count;`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9int main(int argc, char const *argv[]) {`f`b
`B100`F9d9 printf("%f\\n", average(3, 1, 2, 3));`f`b
`B100`F9d9 return 0;`f`b
`B100`F9d9}`f`b
This will compute the average of an arbitrary number of arguments. Note that the function does not know the number of arguments or their types. The above function expects that the types will be `B100`F9d9int`f`b, and that the number of arguments is passed in the first argument (this is a frequent usage but by no means enforced by the language or compiler). In some other cases, for example `F33f`_`[printf`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Printf]`_`f, the number and types of arguments are figured out from a format string. In both cases, this depends on the programmer to supply the correct information. (Alternatively, a `F33f`_`[sentinel value`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Sentinel_value]`_`f like `B100`F9d9NULL`f`b or `B100`F9d9nullptr`f`b may be used to indicate the end of the parameter list.) If fewer arguments are passed in than the function believes, or the types of arguments are incorrect, this could cause it to read into invalid areas of memory and can lead to vulnerabilities like the `F33f`_`[format string attack`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Format_string_attack]`_`f. Depending on the system, even using `B100`F9d9NULL`f`b as a sentinel may encounter such problems; `B100`F9d9nullptr`f`b or a dedicated null pointer of the correct target type may be used to avoid them.
`B100`F9d9stdarg.h`f`b declares a type, `B100`F9d9va_list`f`b, and defines four macros: `F33f`_`[va_start`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Va_start]`_`f, `F33f`_`[va_arg`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Va_arg]`_`f, `F33f`_`[va_copy`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Va_copy]`_`f, and `F33f`_`[va_end`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Va_end]`_`f. Each invocation of `B100`F9d9va_start`f`b and `B100`F9d9va_copy`f`b must be matched by a corresponding invocation of `B100`F9d9va_end`f`b. When working with variable arguments, a function normally declares a variable of type `B100`F9d9va_list`f`b (`B100`F9d9ap`f`b in the example) that will be manipulated by the macros.
1. `B100`F9d9va_start`f`b takes two arguments, a `B100`F9d9va_list`f`b object and a reference to the function's last parameter (the one before the ellipsis; the macro uses this to get its bearings). In `F33f`_`[C23`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C23_(C_standard_revision)]`_`f, the second argument will no longer be required and variadic functions will no longer need a named parameter before the ellipsis.`:cite-ref-kandr-6-0[`F5bf`_`[note 1`#cite-note-kandr-6]`_`f]`:cite-ref-n2975-7-0[`F5bf`_`[6`#cite-note-n2975-7]`_`f] It initialises the `B100`F9d9va_list`f`b object for use by `B100`F9d9va_arg`f`b or `B100`F9d9va_copy`f`b. The compiler will normally issue a warning if the reference is incorrect (e.g. a reference to a different parameter than the last one, or a reference to a wholly different object), but will not prevent compilation from completing normally.
2. `B100`F9d9va_arg`f`b takes two arguments, a `B100`F9d9va_list`f`b object (previously initialised) and a type descriptor. It expands to the next variable argument, and has the specified type. Successive invocations of `B100`F9d9va_arg`f`b allow processing each of the variable arguments in turn. Unspecified behavior occurs if the type is incorrect or there is no next variable argument.
3. `B100`F9d9va_end`f`b takes one argument, a `B100`F9d9va_list`f`b object. It serves to clean up. If one wanted to, for instance, scan the variable arguments more than once, the programmer would re-initialise your `B100`F9d9va_list`f`b object by invoking `B100`F9d9va_end`f`b and then `B100`F9d9va_start`f`b again on it.
4. `B100`F9d9va_copy`f`b takes two arguments, both of them `B100`F9d9va_list`f`b objects. It clones the second (which must have been initialised) into the first. Going back to the "scan the variable arguments more than once" example, this could be achieved by invoking `B100`F9d9va_start`f`b on a first `B100`F9d9va_list`f`b, then using `B100`F9d9va_copy`f`b to clone it into a second `B100`F9d9va_list`f`b. After scanning the variable arguments a first time with `B100`F9d9va_arg`f`b and the first `B100`F9d9va_list`f`b (disposing of it with `B100`F9d9va_end`f`b), the programmer could scan the variable arguments a second time with `B100`F9d9va_arg`f`b and the second `B100`F9d9va_list`f`b. `B100`F9d9va_end`f`b needs to also be called on the cloned `B100`F9d9va_list`f`b before the containing function returns.
>>>In C#
`F33f`_`[C#`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=C_Sharp_(programming_language)]`_`f describes variadic functions using the `B100`F9d9params`f`b keyword. A type must be provided for the arguments, although `B100`F9d9object[]`f`b can be used as a catch-all. At the calling site, you can either list the arguments one by one, or hand over a pre-existing array having the required element type. Using the variadic form is `F33f`_`[Syntactic sugar`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Syntactic_sugar]`_`f for the latter.
`B100`F9d9using System;`f`b
`B100`F9d9`f`b
`B100`F9d9class Program`f`b
`B100`F9d9{`f`b
`B100`F9d9 static int Foo(int a, int b, params int[] args)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 // Return the sum of the integers in args, ignoring a and b.`f`b
`B100`F9d9 int sum = 0;`f`b
`B100`F9d9 foreach (int i in args)`f`b
`B100`F9d9 sum += i;`f`b
`B100`F9d9 return sum;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 static void Main(string[] args)`f`b
`B100`F9d9 {`f`b
`B100`F9d9 Console.WriteLine(Foo(1, 2)); // 0`f`b
`B100`F9d9 Console.WriteLine(Foo(1, 2, 3, 10, 20)); // 33`f`b
`B100`F9d9 int[] manyValues = new int[] { 13, 14, 15 };`f`b
`B100`F9d9 Console.WriteLine(Foo(1, 2, manyValues)); // 42`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
>>>In C++
The basic variadic facility in C++ is largely identical to that in C. The only difference is in the syntax, where the comma before the ellipsis can be omitted. C++ allows variadic functions without `F33f`_`[named parameters`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Named_parameter]`_`f but provides no way to access those arguments since `B100`F9d9va_start`f`b requires the name of the last fixed argument of the function.
`B100`F9d9#include <iostream>`f`b
`B100`F9d9#include <cstdarg>`f`b
`B100`F9d9`f`b
`B100`F9d9void simple_printf(const char* fmt...) // C-style "const char* fmt, ..." is also valid`f`b
`B100`F9d9{`f`b
`B100`F9d9 va_list args;`f`b
`B100`F9d9 va_start(args, fmt);`f`b
`B100`F9d9`f`b
`B100`F9d9 while (*fmt != '\\0') {`f`b
`B100`F9d9 if (*fmt == 'd') {`f`b
`B100`F9d9 int i = va_arg(args, int);`f`b
`B100`F9d9 std::cout << i << '\\n';`f`b
`B100`F9d9 } else if (*fmt == 'c') {`f`b
`B100`F9d9 // note automatic conversion to integral type`f`b
`B100`F9d9 int c = va_arg(args, int);`f`b
`B100`F9d9 std::cout << static_cast<char>(c) << '\\n';`f`b
`B100`F9d9 } else if (*fmt == 'f') {`f`b
`B100`F9d9 double d = va_arg(args, double);`f`b
`B100`F9d9 std::cout << d << '\\n';`f`b
`B100`F9d9 }`f`b
`B100`F9d9 ++fmt;`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 va_end(args);`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9int main()`f`b
`B100`F9d9{`f`b
`B100`F9d9 simple_printf("dcff", 3, 'a', 1.999, 42.5);`f`b
`B100`F9d9}`f`b
`F33f`_`[Variadic templates`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variadic_templates]`_`f (parameter pack) can also be used in C++ with language built-in `F33f`_`[fold expressions`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Fold_(higher-order_function)]`_`f.
`B100`F9d9#include <iostream>`f`b
`B100`F9d9`f`b
`B100`F9d9template <typename... Ts>`f`b
`B100`F9d9void foo_print(Ts... args)`f`b
`B100`F9d9{`f`b
`B100`F9d9 ((std::cout << args << ' '), ...);`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9int main()`f`b
`B100`F9d9{`f`b
`B100`F9d9 std::cout << std::boolalpha;`f`b
`B100`F9d9 foo_print(1, 3.14f); // 1 3.14`f`b
`B100`F9d9 foo_print("Foo", 'b', true, nullptr); // Foo b true nullptr`f`b
`B100`F9d9}`f`b
The `F33f`_`[CERT Coding Standards`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=CERT_Coding_Standards]`_`f for C++ strongly prefers the use of `F33f`_`[variadic templates`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variadic_templates]`_`f (parameter pack) in C++ over the C-style variadic function due to a lower risk of misuse.`:cite-ref-8[`F5bf`_`[7`#cite-note-8]`_`f]
>>>In Fortran
Since the Fortran 90 revision, `F33f`_`[Fortran`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Fortran]`_`f functions or subroutines can accept optional arguments:`:cite-ref-9[`F5bf`_`[8`#cite-note-9]`_`f] the argument list is still fixed, but the ones that have the `B100`F9d9optional`f`b attribute can be omitted in the function/subroutine call. The intrinsic function `B100`F9d9present()`f`b can be used to detect the presence of an optional argument. The optional arguments can appear anywhere in the argument list.
`B100`F9d9program test`f`b
`B100`F9d9implicit none`f`b
`B100`F9d9`f`b
`B100`F9d9 real :: x`f`b
`B100`F9d9`f`b
`B100`F9d9 !> all arguments are passed:`f`b
`B100`F9d9 call foo( 1, 2, 3.0, 4, x )`f`b
`B100`F9d9 !< outputs 1 \\ 2 \\ 3.0 \\ 4 \\ 6.0 (the "\\" denotes a newline)`f`b
`B100`F9d9`f`b
`B100`F9d9 !> the last 2 arguments are omitted:`f`b
`B100`F9d9 call foo( 1, 2, 3.0 )`f`b
`B100`F9d9 !< outputs 1 \\ 2 \\ 3.0`f`b
`B100`F9d9`f`b
`B100`F9d9 !> the 2nd and 4th arguments are omitted: the arguments that are positioned after`f`b
`B100`F9d9 !> an omitted argument must be passed with a keyword:`f`b
`B100`F9d9 call foo( 1, c=3.0, e=x )`f`b
`B100`F9d9 !< outputs 1 \\ 3.0 \\ 6.0`f`b
`B100`F9d9`f`b
`B100`F9d9 !> alternatively, the Fortran 2023 revision has introduced the .NIL. pseudo constant`f`b
`B100`F9d9 !> to denote an omitted argument`f`b
`B100`F9d9 call foo( 1, .NIL., 3.0, .NIL., x )`f`b
`B100`F9d9 !< outputs 1 \\ 3.0 \\ 6.0`f`b
`B100`F9d9`f`b
`B100`F9d9contains`f`b
`B100`F9d9`f`b
`B100`F9d9 !> the subroutine foo() has 2 mandatory and 3 optional arguments`f`b
`B100`F9d9 subroutine foo( a, b, c, d, e )`f`b
`B100`F9d9 integer, intent(in) :: a`f`b
`B100`F9d9 integer, intent(in), optional :: b`f`b
`B100`F9d9 real, intent(in) :: c`f`b
`B100`F9d9 integer, intent(in), optional :: d`f`b
`B100`F9d9 real, intent(out), optional :: e`f`b
`B100`F9d9`f`b
`B100`F9d9 print*, a`f`b
`B100`F9d9 if (present(b)) print*, b`f`b
`B100`F9d9 print*, c`f`b
`B100`F9d9 if (present(d)) print*, d`f`b
`B100`F9d9 if (present(e)) then`f`b
`B100`F9d9 e = 2*c`f`b
`B100`F9d9 print*, c`f`b
`B100`F9d9 end if`f`b
`B100`F9d9 end subroutine`f`b
`B100`F9d9`f`b
`B100`F9d9end program`f`b
`!Output:`!
`B100`F9d9The sum of [1 2] is 3`f`b
`B100`F9d9The sum of [1 2 3] is 6`f`b
`B100`F9d9The sum of [1 2 3 4] is 10`f`b
>>>In Go
Variadic functions in `F33f`_`[Go`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Go_(programming_language)]`_`f can be called with any number of trailing arguments.`:cite-ref-10[`F5bf`_`[9`#cite-note-10]`_`f] `B100`F9d9fmt.Println`f`b is a common variadic function; it uses an empty interface as a catch-all type.
`B100`F9d9package main`f`b
`B100`F9d9`f`b
`B100`F9d9import "fmt"`f`b
`B100`F9d9`f`b
`B100`F9d9// This variadic function takes an arbitrary number of ints as arguments.`f`b
`B100`F9d9func sum(nums ...int) {`f`b
`B100`F9d9 fmt.Print("The sum of ", nums) // Also a variadic function.`f`b
`B100`F9d9 total := 0`f`b
`B100`F9d9 for _, num := range nums {`f`b
`B100`F9d9 total += num`f`b
`B100`F9d9 }`f`b
`B100`F9d9 fmt.Println(" is", total) // Also a variadic function.`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9func main() {`f`b
`B100`F9d9 // Variadic functions can be called in the usual way with individual`f`b
`B100`F9d9 // arguments.`f`b
`B100`F9d9 sum(1, 2) // "The sum of [1 2] is 3"`f`b
`B100`F9d9 sum(1, 2, 3) // "The sum of [1 2 3] is 6"`f`b
`B100`F9d9`f`b
`B100`F9d9 // If you already have multiple args in a slice, apply them to a variadic`f`b
`B100`F9d9 // function using func(slice...) like this.`f`b
`B100`F9d9 nums := []int{1, 2, 3, 4}`f`b
`B100`F9d9 sum(nums...) // "The sum of [1 2 3 4] is 10"`f`b
`B100`F9d9}`f`b
`!Output:`!
`B100`F9d9The sum of [1 2] is 3`f`b
`B100`F9d9The sum of [1 2 3] is 6`f`b
`B100`F9d9The sum of [1 2 3 4] is 10`f`b
>>>In Java
As with C#, the `B100`F9d9Object`f`b type in `F33f`_`[Java`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_(programming_language)]`_`f is available as a catch-all.
`B100`F9d9public class Program {`f`b
`B100`F9d9 // Variadic methods store any additional arguments they receive in an array.`f`b
`B100`F9d9 // Consequentially, \`printArgs\` is actually a method with one parameter: a`f`b
`B100`F9d9 // variable-length array of \`String\`s.`f`b
`B100`F9d9 private static void printArgs(String... strings) {`f`b
`B100`F9d9 for (String string : strings) {`f`b
`B100`F9d9 System.out.println(string);`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 public static void main(String[] args) {`f`b
`B100`F9d9 printArgs("hello"); // short for printArgs(["hello"])`f`b
`B100`F9d9 printArgs("hello", "world"); // short for printArgs(["hello", "world"])`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
>>>In JavaScript
`F33f`_`[JavaScript`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaScript]`_`f does not care about types of variadic arguments.
`B100`F9d9function sum(...numbers) {`f`b
`B100`F9d9 return numbers.reduce((a, b) => a + b, 0);`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9console.log(sum(1, 2, 3)); // 6`f`b
`B100`F9d9console.log(sum(3, 2)); // 5`f`b
`B100`F9d9console.log(sum()); // 0`f`b
It's also possible to create a variadic function using the arguments object, although it is only usable with functions created with the `B100`F9d9function`f`b keyword.
`B100`F9d9function sum() {`f`b
`B100`F9d9 return Array.prototype.reduce.call(arguments, (a, b) => a + b, 0);`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9console.log(sum(1, 2, 3)); // 6`f`b
`B100`F9d9console.log(sum(3, 2)); // 5`f`b
`B100`F9d9console.log(sum()); // 0`f`b
>>>In Lua
`F33f`_`[Lua`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lua_(programming_language)]`_`f functions may pass varargs to other functions the same way as other values using the `B100`F9d9return`f`b keyword. tables can be passed into variadic functions by using, in Lua version 5.2 or higher`:cite-ref-11[`F5bf`_`[10`#cite-note-11]`_`f] `B100`F9d9table.unpack`f`b, or Lua 5.1 or lower`:cite-ref-12[`F5bf`_`[11`#cite-note-12]`_`f] `B100`F9d9unpack`f`b. Varargs can be used as a table by constructing a table with the vararg as a value.
`B100`F9d9function sum(...) --... designates varargs`f`b
`B100`F9d9 local sum=0`f`b
`B100`F9d9 for _,v in pairs({...}) do --creating a table with a varargs is the same as creating one with standard values`f`b
`B100`F9d9 sum=sum+v`f`b
`B100`F9d9 end`f`b
`B100`F9d9 return sum`f`b
`B100`F9d9end`f`b
`B100`F9d9`f`b
`B100`F9d9values={1,2,3,4}`f`b
`B100`F9d9sum(5,table.unpack(values)) --returns 15. table.unpack should go after any other arguments, otherwise not all values will be passed into the function.`f`b
`B100`F9d9`f`b
`B100`F9d9function add5(...)`f`b
`B100`F9d9 return ...+5 --this is incorrect usage of varargs, and will only return the first value provided`f`b
`B100`F9d9end`f`b
`B100`F9d9`f`b
`B100`F9d9entries={}`f`b
`B100`F9d9function process_entries()`f`b
`B100`F9d9 local processed={}`f`b
`B100`F9d9 for i,v in pairs(entries) do`f`b
`B100`F9d9 processed[i]=v --placeholder processing code`f`b
`B100`F9d9 end`f`b
`B100`F9d9 return table.unpack(processed) --returns all entries in a way that can be used as a vararg`f`b
`B100`F9d9end`f`b
`B100`F9d9`f`b
`B100`F9d9print(process_entries()) --the print function takes all varargs and writes them to stdout separated by newlines`f`b
>>>In Pascal
`F33f`_`[Pascal`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Pascal_(programming_language)]`_`f is standardized by `F33f`_`[ISO`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=International_Organization_for_Standardization]`_`f standards 7185 (“Standard Pascal”) and 10206 (“Extended Pascal”). Neither standardized form of Pascal supports variadic routines, `*except`* for certain `F33f`_`[built-in routines`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Intrinsic_function]`_`f (`B100`F9d9read`f`b/`B100`F9d9readLn`f`b and `B100`F9d9write`f`b/`B100`F9d9writeLn`f`b, and additionally in EP `B100`F9d9readStr`f`b/`B100`F9d9writeStr`f`b).
Nonetheless, `*dialects`* of Pascal implement mechanisms `*resembling`* variadic routines. `F33f`_`[Delphi`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Delphi_(programming_language)]`_`f defines an `B100`F9d9array of const`f`b data type that may be associated with the `*last`* `F33f`_`[formal parameter`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Formal_parameter]`_`f. Within the routine definition the `B100`F9d9array of const`f`b is an `B100`F9d9array of TVarRec`f`b, an `F33f`_`[array`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Array_(data_type)]`_`f of `F33f`_`[variant records`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variant_record]`_`f.`:cite-ref-delphi-13-0[`F5bf`_`[12`#cite-note-delphi-13]`_`f] The `B100`F9d9VType`f`b member of the aforementioned `B100`F9d9record`f`b data type allows inspection of the argument’s data type and subsequent appropriate handling. The `F33f`_`[Free Pascal Compiler`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Free_Pascal_Compiler]`_`f supports Delphi’s variadic routines, too.`:cite-ref-fpc-14-0[`F5bf`_`[13`#cite-note-fpc-14]`_`f]
This implementation, however, technically requires a `*single`* argument, that is an `B100`F9d9array`f`b. Pascal imposes the restriction that arrays need to be homogenous. This requirement is circumvented by utilizing a variant record. The `F33f`_`[GNU Pascal`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=GNU_Pascal]`_`f defines a real variadic formal parameter specification using an ellipsis (`B100`F9d9...`f`b), but as of 2022 no portable mechanism to use such has been defined.`:cite-ref-gpc-15-0[`F5bf`_`[14`#cite-note-gpc-15]`_`f]
Both GNU Pascal and FreePascal allow externally declared functions to use a variadic formal parameter specification using an ellipsis (`B100`F9d9...`f`b).
>>>In PHP
`F33f`_`[PHP`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=PHP]`_`f does not care about types of variadic arguments unless the argument is typed.
`B100`F9d9function sum(...$nums): int`f`b
`B100`F9d9{`f`b
`B100`F9d9 return array_sum($nums);`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9echo sum(1, 2, 3); // 6`f`b
And typed variadic arguments:
`B100`F9d9function sum(int ...$nums): int`f`b
`B100`F9d9{`f`b
`B100`F9d9 return array_sum($nums);`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9echo sum(1, 'a', 3); // TypeError: Argument 2 passed to sum() must be of the type int (since PHP 7.3)`f`b
>>>In Python
`F33f`_`[Python`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Python_(programming_language)]`_`f does not care about types of variadic arguments.
`B100`F9d9def foo(a, b, *args):`f`b
`B100`F9d9 print(args) # args is a tuple (immutable sequence).`f`b
`B100`F9d9`f`b
`B100`F9d9foo(1, 2) # ()`f`b
`B100`F9d9foo(1, 2, 3) # (3,)`f`b
`B100`F9d9foo(1, 2, 3, "hello") # (3, "hello")`f`b
Keyword arguments can be stored in a dictionary, e.g. `B100`F9d9def bar(*args, **kwargs)`f`b.
>>>In Raku
In `F33f`_`[Raku`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Raku_(programming_language)]`_`f, the type of parameters that create variadic functions are known as `*slurpy`* array parameters and they're classified into three groups:
>>>>Flattened slurpy
These parameters are declared with a single asterisk (`B100`F9d9*`f`b) and they flatten arguments by dissolving one or more layers of elements that can be iterated over (i.e, Iterables).
`B100`F9d9sub foo($a, $b, *@args) {`f`b
`B100`F9d9 say @args.perl;`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9foo(1, 2) # []`f`b
`B100`F9d9foo(1, 2, 3) # [3]`f`b
`B100`F9d9foo(1, 2, 3, "hello") # [3 "hello"]`f`b
`B100`F9d9foo(1, 2, 3, [4, 5], [6]); # [3, 4, 5, 6]`f`b
>>>>Unflattened slurpy
These parameters are declared with two asterisks (`B100`F9d9**`f`b) and they do not flatten any iterable arguments within the list, but keep the arguments more or less as-is:
`B100`F9d9sub bar($a, $b, **@args) {`f`b
`B100`F9d9 say @args.perl;`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9bar(1, 2); # []`f`b
`B100`F9d9bar(1, 2, 3); # [3]`f`b
`B100`F9d9bar(1, 2, 3, "hello"); # [3 "hello"]`f`b
`B100`F9d9bar(1, 2, 3, [4, 5], [6]); # [3, [4, 5], [6]]`f`b
>>>>Contextual slurpy
These parameters are declared with a plus (`B100`F9d9+`f`b) sign and they apply the `*"single argument rule"`*, which decides how to handle the slurpy argument based upon context. Simply put, if only a single argument is passed and that argument is iterable, that argument is used to fill the slurpy parameter array. In any other case, `B100`F9d9+@`f`b works like `B100`F9d9**@`f`b (i.e., unflattened slurpy).
`B100`F9d9sub zaz($a, $b, +@args) {`f`b
`B100`F9d9 say @args.perl;`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9zaz(1, 2); # []`f`b
`B100`F9d9zaz(1, 2, 3); # [3]`f`b
`B100`F9d9zaz(1, 2, 3, "hello"); # [3 "hello"]`f`b
`B100`F9d9zaz(1, 2, [4, 5]); # [4, 5], single argument fills up array`f`b
`B100`F9d9zaz(1, 2, 3, [4, 5]); # [3, [4, 5]], behaving as **@`f`b
`B100`F9d9zaz(1, 2, 3, [4, 5], [6]); # [3, [4, 5], [6]], behaving as **@`f`b
>>>In Ruby
`F33f`_`[Ruby`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Ruby_(programming_language)]`_`f does not care about types of variadic arguments.
`B100`F9d9def foo(*args)`f`b
`B100`F9d9 print args`f`b
`B100`F9d9end`f`b
`B100`F9d9`f`b
`B100`F9d9foo(1)`f`b
`B100`F9d9# prints \`[1]=> nil\``f`b
`B100`F9d9`f`b
`B100`F9d9foo(1, 2)`f`b
`B100`F9d9# prints \`[1, 2]=> nil\``f`b
>>>In Rust
`F33f`_`[Rust`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Rust_(programming_language)]`_`f does not support variadic arguments in functions. Instead, it uses `F33f`_`[macros`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Macro_(computer_science)]`_`f.`:cite-ref-16[`F5bf`_`[15`#cite-note-16]`_`f]
`B100`F9d9macro_rules! calculate {`f`b
`B100`F9d9 // The pattern for a single \`eval\``f`b
`B100`F9d9 (eval $e:expr) => {{`f`b
`B100`F9d9 {`f`b
`B100`F9d9 let val: usize = $e; // Force types to be integers`f`b
`B100`F9d9 println!("{} = {}", stringify!{$e}, val);`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }};`f`b
`B100`F9d9`f`b
`B100`F9d9 // Decompose multiple \`eval\`s recursively`f`b
`B100`F9d9 (eval $e:expr, $(eval $es:expr),+) => {{`f`b
`B100`F9d9 calculate! { eval $e }`f`b
`B100`F9d9 calculate! { $(eval $es),+ }`f`b
`B100`F9d9 }};`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9fn main() {`f`b
`B100`F9d9 calculate! { // Look ma! Variadic \`calculate!\`!`f`b
`B100`F9d9 eval 1 + 2,`f`b
`B100`F9d9 eval 3 + 4,`f`b
`B100`F9d9 eval (2 * 3) + 1`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
Rust is able to interact with C's variadic system via a `B100`F9d9c_variadic`f`b feature switch. As with other C interfaces, the system is considered `B100`F9d9unsafe`f`b to Rust.`:cite-ref-17[`F5bf`_`[16`#cite-note-17]`_`f]
>>>In Scala
`B100`F9d9object Program {`f`b
`B100`F9d9 // Variadic methods store any additional arguments they receive in an array.`f`b
`B100`F9d9 // Consequentially, \`printArgs\` is actually a method with one parameter: a`f`b
`B100`F9d9 // variable-length array of \`String\`s.`f`b
`B100`F9d9 private def printArgs(strings: String*): Unit = {`f`b
`B100`F9d9 strings.foreach(println)`f`b
`B100`F9d9 }`f`b
`B100`F9d9`f`b
`B100`F9d9 def main(args: Array[String]): Unit = {`f`b
`B100`F9d9 printArgs("hello"); // short for printArgs(["hello"])`f`b
`B100`F9d9 printArgs("hello", "world"); // short for printArgs(["hello", "world"])`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
>>>In Swift
`F33f`_`[Swift`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Swift_(programming_language)]`_`f cares about the type of variadic arguments, but the catch-all `B100`F9d9Any`f`b type is available.
`B100`F9d9func greet(timeOfTheDay: String, names: String...) {`f`b
`B100`F9d9 // here, names is [String]`f`b
`B100`F9d9`f`b
`B100`F9d9 print("Looks like we have \\(names.count) people")`f`b
`B100`F9d9`f`b
`B100`F9d9 for name in names {`f`b
`B100`F9d9 print("Hello \\(name), good \\(timeOfTheDay)")`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9greet(timeOfTheDay: "morning", names: "Joseph", "Clara", "William", "Maria")`f`b
`B100`F9d9`f`b
`B100`F9d9// Output:`f`b
`B100`F9d9// Looks like we have 4 people`f`b
`B100`F9d9// Hello Joseph, good morning`f`b
`B100`F9d9// Hello Clara, good morning`f`b
`B100`F9d9// Hello William, good morning`f`b
`B100`F9d9// Hello Maria, good morning`f`b
>>>In Tcl
A `F33f`_`[Tcl`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Tcl]`_`f procedure or lambda is variadic when its last argument is `B100`F9d9args`f`b: this will contain a list (possibly empty) of all the remaining arguments. This pattern is common in many other procedure-like methods.`:cite-ref-18[`F5bf`_`[17`#cite-note-18]`_`f]`:cite-ref-19[`F5bf`_`[18`#cite-note-19]`_`f]
`B100`F9d9proc greet {timeOfTheDay args} {`f`b
`B100`F9d9 puts "Looks like we have [llength $args] people"`f`b
`B100`F9d9`f`b
`B100`F9d9 foreach name $args {`f`b
`B100`F9d9 puts "Hello $name, good $timeOfTheDay"`f`b
`B100`F9d9 }`f`b
`B100`F9d9}`f`b
`B100`F9d9`f`b
`B100`F9d9greet "morning" "Joseph" "Clara" "William" "Maria"`f`b
`B100`F9d9`f`b
`B100`F9d9# Output:`f`b
`B100`F9d9# Looks like we have 4 people`f`b
`B100`F9d9# Hello Joseph, good morning`f`b
`B100`F9d9# Hello Clara, good morning`f`b
`B100`F9d9# Hello William, good morning`f`b
`B100`F9d9# Hello Maria, good morning`f`b
>>See also
• `F33f`_`[Varargs in Java programming language`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_syntax]`_`f
• `F33f`_`[Variadic macro`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variadic_macro]`_`f (C programming language)
• `F33f`_`[Variadic template`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variadic_template]`_`f
>>Notes
`:cite-note-kandr-6`!note 1.`! `F0af`_`[↑`#cite-ref-kandr-6-0]`_`f Making the named parameter optional was needed since there was no way to specify a function taking an unspecified number of arguments in C23 after the removal of K&R style function definitions. Since C++ was already using this syntax for the same purpose, this change was also a way to increase compatibility between the languages.`:cite-ref-5[`F5bf`_`[5`#cite-note-5]`_`f]
>>References
`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f Henry S. Leonard and H. N. Goodman, `*A calculus of individuals`*. Abstract of a talk given at the Second Meeting of the Association for Symbolic Logic, held in Cambridge MA on December 28–30, 1936, [1], `*Journal of Symbolic Logic`* `!2`!(1) 1937, 63.
`:cite-note-2`!2.`! `F0af`_`[↑`#cite-ref-2]`_`f `:citerefklemens2014`aKlemens, Ben (2014). `*21st Century C: C Tips from the New School`*. O'Reilly Media, Inc. p. 224. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 978-1491904442.
`:cite-note-3`!3.`! `F0af`_`[↑`#cite-ref-3]`_`f CLP (H): Constraint Logic Programming for Hedges
`:cite-note-4`!4.`! `F0af`_`[↑`#cite-ref-4]`_`f "<cstdarg> (stdarg.h) - C++ Reference". `*www.cplusplus.com`*. Archived from the original on 2012-10-31. Retrieved 2007-10-02.
`:cite-note-5`!5.`! `F0af`_`[↑`#cite-ref-5]`_`f "C23 is Finished: Here is What is on the Menu §N2975 - Relax requirements for variadic parameter lists". 31 July 2022.
`:cite-note-n2975-7`!6.`! `F0af`_`[↑`#cite-ref-n2975-7-0]`_`f `:citerefgildingmeneide2022`aGilding, Alex; Meneide, JeanHeyd (2022-04-15). "WG14-N2975 : Relax requirements for variadic parameter lists, v3" (PDF).
`:cite-note-8`!7.`! `F0af`_`[↑`#cite-ref-8]`_`f "DCL50-CPP. Do not define a C-style variadic function".
`:cite-note-9`!8.`! `F0af`_`[↑`#cite-ref-9]`_`f "Optional Arguments". `*Intel`*. Retrieved 2025-03-18.
`:cite-note-10`!9.`! `F0af`_`[↑`#cite-ref-10]`_`f "Go by Example: Variadic Functions".
`:cite-note-11`!10.`! `F0af`_`[↑`#cite-ref-11]`_`f "Lua 5.2 Reference Manual". `*www.lua.org`*. Retrieved 2023-02-05.
`:cite-note-12`!11.`! `F0af`_`[↑`#cite-ref-12]`_`f "Lua 5.1 Reference Manual". `*www.lua.org`*. Retrieved 2023-02-05.
`:cite-note-delphi-13`!12.`! `F0af`_`[↑`#cite-ref-delphi-13-0]`_`f "Parameters (Delphi)". Retrieved 2023-08-28.
`:cite-note-fpc-14`!13.`! `F0af`_`[↑`#cite-ref-fpc-14-0]`_`f "Free Pascal - Reference guide". Retrieved 2023-08-28.
`:cite-note-gpc-15`!14.`! `F0af`_`[↑`#cite-ref-gpc-15-0]`_`f "The GNU Pascal Manual". Retrieved 2023-08-28.
`:cite-note-16`!15.`! `F0af`_`[↑`#cite-ref-16]`_`f "Variadics". `*Rust By Example`*.
`:cite-note-17`!16.`! `F0af`_`[↑`#cite-ref-17]`_`f "2137-variadic". `*The Rust RFC Book`*.
`:cite-note-18`!17.`! `F0af`_`[↑`#cite-ref-18]`_`f "proc manual page". `*Tcl/Tk Documentation`*.
`:cite-note-19`!18.`! `F0af`_`[↑`#cite-ref-19]`_`f "args". `*Tcler's Wiki`*.
>>External links
• Variadic function. `F33f`_`[Rosetta Code`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Rosetta_Code]`_`f task showing the implementation of variadic functions in over 120 programming languages.
• Variable Argument Functions — A tutorial on Variable Argument Functions for C++
• GNU libc manual
`c`F0af`_`[↑ Back to top`#top]`_`f`a